﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class ExogenesisItemMeido : EditorWindow {

	private enum LEFT_VERT_MODES {ALL_ITEMS, COMBINATIONS}
	private static LEFT_VERT_MODES Left_Vert_Mode = LEFT_VERT_MODES.ALL_ITEMS;

	private static Dictionary<int, ExogenesisItem> CurrentItems;
	private static List<ItemCombination> CurrentCombinations;
	private static int LastID = -1;

	private ExogenesisItem currentItem = null;
	private ItemCombination currentCombination = null;
	private string debugString = "Debug goes here";

	void OnGUI(){
	
		#region Top Horizontal
		Rect TopHorizontal = EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(300), GUILayout.MaxHeight(500));

		#region Left Vertical
		Rect LeftVert = EditorGUILayout.BeginVertical(GUILayout.MinWidth(150), GUILayout.MaxWidth(250));

		EditorGUILayout.BeginHorizontal();
		if(GUILayout.Button("Edit Items"))
			Left_Vert_Mode = LEFT_VERT_MODES.ALL_ITEMS;
		if(GUILayout.Button("Edit Combinations"))
			Left_Vert_Mode = LEFT_VERT_MODES.COMBINATIONS;
		EditorGUILayout.EndHorizontal();

		switch(Left_Vert_Mode)
		{
			case LEFT_VERT_MODES.ALL_ITEMS:
				RederAllItemsLeft(); 
			break;

			case LEFT_VERT_MODES.COMBINATIONS:
				RenderAllCombinationsLeft();
			break;
		}

		if(GUILayout.Button("Save"))
		{
			ItemHelper.SaveItemList();
			AssetDatabase.Refresh();
		}

		EditorGUILayout.EndVertical();
		#endregion

		#region Left Vertical
		Rect RightVert = EditorGUILayout.BeginVertical();

		switch(Left_Vert_Mode)
		{
			case LEFT_VERT_MODES.ALL_ITEMS:
				RenderSelectedItem();
				break;
				
			case LEFT_VERT_MODES.COMBINATIONS:
				RenderSelectedCombination();
				break;
		}

		
		EditorGUILayout.EndVertical();
		#endregion

		EditorGUILayout.EndHorizontal();
		#endregion


		RenderDebug();
	}

	void RederAllItemsLeft()
	{
		EditorGUILayout.LabelField("Current Items");

		EditorGUILayout.BeginHorizontal();
		if(GUILayout.Button("+"))
		{
			ExogenesisItem newItem = new ExogenesisItem();
			newItem.ID = LastID;
			CurrentItems.Add(newItem.ID,newItem);
			LastID++;
			
			currentItem = newItem;
		}
		
		if(GUILayout.Button("-"))
		{
			//CurrentItems.Add(new ExogenesisItem());

			if(currentItem != null)
			{
				CurrentItems.Remove(currentItem.ID);
				currentItem = null;
				Debug.Log("Removing " + currentItem.ID);
				debugString = "Removed " + currentItem.Name;
			}
		}
		EditorGUILayout.EndHorizontal();
		
		RenderCurrentItemList();

	}

	void RenderAllCombinationsLeft()
	{
		EditorGUILayout.LabelField("Current Combnations");

		EditorGUILayout.BeginHorizontal();
		if(GUILayout.Button("+"))
		{
			//TODO: Add combination here-
		}
		
		if(GUILayout.Button("-"))
		{
			//TODO: Remove combination here~
		}
		EditorGUILayout.EndHorizontal();

		RenderCurrentCombinationList();
	}

	void RenderCurrentCombinationList()
	{
		if(CurrentCombinations != null)
		{
			foreach(var exo_combination in CurrentCombinations)
			{
				if(GUILayout.Button(exo_combination.Item_Product.Name))
				{
					currentCombination = exo_combination;
					debugString = "Current Combination for " + currentCombination.Item_Product.Name + " has " + currentCombination.Item_Ingredients.Count + " components";
				}
			}
		}
	}

	void RenderCurrentItemList()
	{
		if(CurrentItems != null)
		{
			foreach(var exo_item in CurrentItems)
			{
				if(GUILayout.Button(exo_item.Value.Name))
				{
					currentItem = exo_item.Value;
					debugString = "Current Item " + currentItem.Name;
				}
			}
		}
	}

	void RenderSelectedItem()
	{
		if(currentItem != null)
		{
			EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField("Name: ", GUILayout.Width(80));
				currentItem.Name = EditorGUILayout.TextField(currentItem.Name);
			EditorGUILayout.EndHorizontal();

			EditorGUILayout.BeginHorizontal();
			EditorGUILayout.LabelField("Image Path " + currentItem.ImagePath);
				if(GUILayout.Button("Open File", GUILayout.Width(150)))
				{
					string path = EditorUtility.OpenFilePanel("Item Image",Application.dataPath, "");

					if(!string.IsNullOrEmpty(path))
					{
						currentItem.ImagePath = SanitizeFilePath(path);
					}
				}
			EditorGUILayout.EndHorizontal();

			EditorGUILayout.LabelField("Desciption: ");
			currentItem.Description = EditorGUILayout.TextArea(currentItem.Description, GUILayout.Height(60));

			if(!string.IsNullOrEmpty(currentItem.ImagePath))
			{
				Texture tex = Resources.Load("Items/" + currentItem.ImagePath, typeof(Texture)) as Texture;
				Rect imageWindow = EditorGUILayout.BeginHorizontal(GUILayout.Height(250), GUILayout.Width(250));

				EditorGUILayout.LabelField("Sample");

				EditorGUI.DrawPreviewTexture(imageWindow,tex );
				EditorGUILayout.EndHorizontal();
			}
		}
	}

	string SanitizeFilePath(string originalPath)
	{
		string ret = originalPath;

		ret = ret.Substring(ret.LastIndexOf("/") + 1);
		ret = ret.Substring(0, ret.LastIndexOf("."));

		return ret;
	}

	void RenderSelectedCombination()
	{
		if(currentCombination != null)
		{
			Debug.Log(currentCombination.Item_Product.Name + " len " + currentCombination.GetIngredients.Length + " in");

			EditorGUILayout.BeginHorizontal(GUILayout.Height(60));
			EditorGUILayout.LabelField(currentCombination.GetIngredients[0].Name);
			EditorGUILayout.LabelField(" + ", GUILayout.Width(25));
			EditorGUILayout.LabelField(currentCombination.GetIngredients[1].Name);
			EditorGUILayout.LabelField(" = ", GUILayout.Width(25));
			EditorGUILayout.LabelField(currentCombination.Item_Product.Name);
			EditorGUILayout.EndHorizontal();

			EditorGUILayout.BeginHorizontal();
			EditorGUILayout.EndHorizontal();
		}
	}

	void RenderDebug()
	{
		#region Bot Horizontal - For the debugs
		Rect BotHorizontal = EditorGUILayout.BeginHorizontal();
		
		EditorGUILayout.LabelField(debugString);
		
		EditorGUILayout.EndHorizontal();
		#endregion

	}

	[MenuItem ("Exogenesis/Item Editor")]
	static void Init(){
		EditorStyles.textField.wordWrap = true;

		CurrentItems = new Dictionary<int, ExogenesisItem>();
		CurrentCombinations = new List<ItemCombination>();
		ItemHelper.LoadItemList();

		CurrentItems = ItemHelper.EXOGENESIS_ITEMS;
		CurrentCombinations = ItemHelper.ITEM_COMBINATIONS;

		Debug.Log(CurrentItems.Count + " items found");
		Debug.Log(CurrentCombinations.Count + " combination found");

		foreach(var item in CurrentItems)
		{
			if(item.Key > LastID)
				LastID = item.Key;
		}

		LastID++;

		Left_Vert_Mode= LEFT_VERT_MODES.ALL_ITEMS;

		ExogenesisItemMeido window = (ExogenesisItemMeido)EditorWindow.GetWindow(typeof(ExogenesisItemMeido));
	
	}
}
